package com.js.mytransitiondemo.transition;

// Created by JS on 2021/7/7.

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.transition.TransitionValues;
import android.transition.Visibility;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

public class OtherTransition extends Visibility {

    private Context context;
    private View rootView;
    private View carView;

    private final int carDis = 500;

    public OtherTransition(Context context, View rootView, View view) {
        this.context = context;
        this.rootView = rootView;
        this.carView = view;
    }

    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        super.captureStartValues(transitionValues);

    }

    @Override
    public void captureEndValues(TransitionValues transitionValues) {
        super.captureEndValues(transitionValues);
    }

    @Override
    public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues) {
        if (view != null) {
            ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
            animator.setInterpolator(new LinearInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float animatedValue = (float) animation.getAnimatedValue();
                    rootView.setAlpha(animatedValue);

                    carView.setTranslationY(carDis - animatedValue * carDis);
                }
            });
            return animator;
        }
        return super.onAppear(sceneRoot, view, startValues, endValues);
    }

    @Override
    public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues) {
        if (view != null) {
            ValueAnimator animator = ValueAnimator.ofFloat(1, 0);
            animator.setInterpolator(new LinearInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float animatedValue = (float) animation.getAnimatedValue();
                    rootView.setAlpha(animatedValue);

                    carView.setTranslationY(carDis - animatedValue * carDis);
                }
            });
            return animator;
        }
        return super.onDisappear(sceneRoot, view, startValues, endValues);
    }
}
